package com.User.aspect;


import com.User.entity.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogginAspect {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Before("execution(* com.User.Controller.UserController.login(com.User.entity.User)) && args(user)")
    public void logBeforeLogin(JoinPoint joinPoint, User user) {
        if (user != null && user.getUsername() != null) {
            log.info("{}用户正在登录", user.getUsername());
        } else {
            log.warn("登录请求中未提供用户名");
        }
    }

    @After("execution(* com.User.Controller.UserController.*(..))")
    public void logAfter(JoinPoint joinPoint) {
        log.info("登录Exiting method: {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
    }

    @Around("execution(* com.User.Controller.UserController.*(..))")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long elapsedTime = System.currentTimeMillis() - start;
        log.info("Method {}.{} execution time: {} ms", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), elapsedTime);
        return result;
    }


}
